






/*
Charles S. Bullock, III, William D. Hicks, 
    M.V. Hood, III, Seth C. McKee, and Daniel A. Smith
   "The Election of African American State Legislators in the Modern South" in LSQ
*/

/*
.DO file created by William D. Hicks (hickswd@appstate.edu)

It reproduces TABLE 4 and FIGURE 3 in the main manuscript. It also reproduces
TABLE D1 and TABLE D2 in the Appendix, as well as FIGURE E1 in Appendix.  
 
Last update to this file: 3/15/2020 
This file directs the following tasks:
   (1) label/define variables
   (2) reproduce MODELS for TABLE 4
   (3) reporoduce TABLE 4 in a .CSV file 
       - TABLE_4.csv
   (4) reproduce FIGURE 3 in a .pdf
       - FIGURE_3.pdf
   (5) reproduce FIGURE E1 in a .pdf
       - FIGURE_E1.pdf
   (6) reproduce MODELS for TABLE D2
   (7) reporoduce TABLE D2 in a .CSV file 
       - TABLE_D2.csv
   (8) reproduce PANEL UNIT ROOT TESTS discussed in Appendix   
   (9) reproduce GRANGER CAUSALITY TESTS discussed in Appendix   
   (10) reproduce SERIES COINTEGRATION TESTS discussed in Appendix   
   (11) reproduce MODELS for TABLE D1
   (12) reporoduce TABLE D1 in a .CSV file 
       - TABLE_D1.csv
   (13) reproduce alternative error correction approach discussed in Appendix
   (14) reproduce Arello and Bond estimator discussed in Appendix 
*/

/*
                 YOU NEED TO FIRST IMPORT THE .DTA DATASET
                 THIS REQUIRES THAT YOU WRITE YOUR OWN PATH
*/

use ~[FILE PATH]/REPLICATION_DATA_LEGISLATORS_FULL.dta, clear 

/*
            OUTPUT (ALL TABLES & GRAPHS) SAVED TO ~DIRECTORY
    -you also need to set the appropriate location for a directory file - 
*/

cd [MAKE YOUR DIRECTORY HERE]



label var state_chamber "Unique state, chamber identifier" 
label var chamber_year "Unique chamber, year identifier" 
label var state_code  "Unique state identifier" 
label var state "State Abbreviation"
label var year "Election Year"
label var chamber "Chamber identifier (8=upper, 9=lower)"
label var p_dem "Proportion of districts electing Democrats"
label var p_rep "Proportion of districts electing Republicans" 
label var p_black "Proportion of districts electing African Americans" 
label var p_majority_minority "Proportion of majority black districts"
label var masssociallib_est "Public opinion social liberalism (Caughey and Warshaw 2018)"
label var masseconlib_est "Public opinion economic liberalism (Caughey and Warshaw 2018)" 
label var dempid_est "Proportion of citizens who identify as Democrats" 
label var reppid_est "Proportion of citizens who identify as Republicans" 
label var pct_black "Percent of citizens who are African American" 
label var i_time_CY "Election year counter, 1 to n for each chamber, election year" 
 


/*GROWTH CURVE MODELS*/

xtset state_chamber i_time_CY
gen p_black_pct = p_black*100
gen lp_black = l.p_black
gen p_rep_pct = p_rep*100
gen lp_black_pct = l.p_rep_pct
gen p_majority_minority_pct = p_majority_minority*100 
gen lp_majority_minority_pct =l.p_majority_minority_pct
gen lmasssociallib = l.masssociallib_est
gen lmasseconlib = l.masseconlib_est
gen lreppid = l.reppid_est
gen lreppid_pct = l.reppid_est*100

/*UNIVERSAL TIME COUNTERS*/

gen u_time_CY = year-1970 if pct_black!=.
gen u_time_CY2 = year-1971 if p_majority_minority!=.


/*ssc install labutil*/
labmask u_time_CY2, values(year)
labmask u_time_CY, values(year)


/* MODELS FOR TABLE 4*/
qui mixed p_rep_pct ///
      c.u_time_CY2 ///
      c.u_time_CY2#c.u_time_CY2 ///
	  c.u_time_CY2#c.u_time_CY2#c.u_time_CY2 ///
	  c.p_majority_minority_pct ///
	  c.pct_black ///
	  c.lmasssociallib c.lmasseconlib c.lreppid_pct ///
      || state_chamber: , mle cov(unst)
estat icc
estadd scalar icc2 = r(icc2)
estimates store M1_RI


qui mixed p_rep_pct ///
      c.u_time_CY2 ///
      c.u_time_CY2#c.u_time_CY2 ///
	  c.u_time_CY2#c.u_time_CY2#c.u_time_CY2 ///
	  c.p_majority_minority_pct c.p_majority_minority_pct#c.p_majority_minority_pct ///
	  c.p_majority_minority_pct#c.p_majority_minority_pct#c.p_majority_minority_pct ///
	  c.pct_black ///
	  c.lmasssociallib c.lmasseconlib c.lreppid_pct ///
      || state_chamber: , mle cov(unst)
estat icc
estadd scalar icc2 = r(icc2)
estimates store M3_RI	  

qui mixed p_rep_pct ///
      c.u_time_CY2 ///
      c.u_time_CY2#c.u_time_CY2 ///
	  c.u_time_CY2#c.u_time_CY2#c.u_time_CY2 ///
	  c.p_majority_minority_pct c.p_majority_minority_pct#c.p_majority_minority_pct ///
	  c.p_majority_minority_pct#c.p_majority_minority_pct#c.p_majority_minority_pct ///
	  c.pct_black ///
	  c.lmasssociallib c.lmasseconlib c.lreppid_pct ///
      || state_chamber: u_time_CY, mle cov(unst)  
estimates store M3_RC	

/* TABLE 4*/
 esttab M1_RI M3_RI M3_RC using TABLE_4.csv, replace ///
 nonumbers mtitles("M1" "M2" "M3") cells(b(star fmt(%9.4f)) ///
 se(par)) starlevels(+ 0.10 * 0.05 ** 0.01) stats(N aic bic icc2, ///
 fmt(%9.0g %9.3f)) varwidth(30) modelwidth(10) ///
 title("Multilevel Growth Model: % GOP Lawmakers Republican") ///
 note("Note: DV = % of lawmakers elected as GOP.") ///
 varlabels(_cons constant u_time_CY2 "Yearly Counter (0=1971)" ///
 c.u_time_CY2#c.u_time_CY2 "Yearly Counter Squared" ///
 c.u_time_CY2#c.u_time_CY2#c.u_time_CY2 "Yearly Counter Cubed" ///
 p_majority_minority_pct "% Districts Majority-Black" ///
 c.p_majority_minority_pct#c.p_majority_minority_pct "% Districts Majority-Black (Squared)" ///
 c.p_majority_minority_pct#c.p_majority_minority_pct#c.p_majority_minority_pct "% Districts Majority-Black (Cubed)" ///
 pct_black "% States' Population Black" ///
 lmasssociallib "Mass Social Liberalism (t-1)" ///
 lmasseconlib "Mass Economic Liberalism (t-1)" ///
 lreppid_pct "% of Republican State Citizens (t-1)") ///
 drop() ///
 order(u_time_CY2 c.u_time_CY2#c.u_time_CY2 ///
 c.u_time_CY2#c.u_time_CY2#c.u_time_CY2 p_majority_minority_pct ///
 c.p_majority_minority_pct#c.p_majority_minority_pct c.p_majority_minority_pct#c.p_majority_minority_pct#c.p_majority_minority_pct) ///
 eqlabels("" "var(Chamber)" "var(Year)" "var(Beta Time)" "Corr(Time Chamber)", none) ///
 transform(ln*: exp(@)^2 exp(@)^2 at*: tanh(@) (1-tanh(@)^2)) ///
 legend style(tab) 
 
 /* FIGURE 3 */

estimates restore M3_RC
qui margins, at(p_majority_minority_pct=(0(2)35) u_time_CY2 =(24) ///
             pct_black=(18.82658 ) lmasssociallib=(-.178985) ///
			 lmasseconlib=(-.0649595) lreppid=(26.8089)) 
marginsplot, scheme(s2mono) ylabel(, nogrid) ///
   plotregion(lcolor(black) lwidth(thin) lpattern(solid)) ///
   yscale( noline) xscale(noline) xsca(titlegap(0)) ysca(titlegap(0)) ///
   ylabel( , labsize(small)) ///
   xlabel(0(5)35, labsize(small) angle(0)) ///
   title("") ytitle("Predicted Share of GOP Seats", size(small)) ///
   xtitle("Share of Majority Minority Districts", size(small)) ///
   recast(line) plot1opts(lcolor(black) lpattern(solid)) ///
   ci1opts(lcolor(gs9) lwidth(vthin) lpattern(dash)) ///
   graphregion(fcolor(white) icolor(white) lcolor(white)) ///
   level(90)  ///
   legend(off) 
graph export FIGURE_3.pdf, replace


/* FIGURE E1 IN THE APPENDIX MATERIAL */ 
estimates restore M3_RC  
qui margins, at(p_majority_minority=(12.12121 ) u_time_CY2 =(0(2)44) ///
             pct_black=(18.82658 ) lmasssociallib=(-.178985) ///
			 lmasseconlib=(-.0649595) lreppid=(26.8089)) 
marginsplot, scheme(s2mono) ylabel(, nogrid) ///
   plotregion(lcolor(black) lwidth(thin) lpattern(solid)) ///
   yscale( noline) xscale(noline) xsca(titlegap(0)) ysca(titlegap(0)) ///
   ylabel(25(5)55 , labsize(vsmall)) ///
   xlabel(0(4)44, labsize(vsmall) angle(45)) ///
   title("") ytitle("Predicted Share of GOP Seats", size(small)) ///
   xtitle("Year") ///
   recast(line) plot1opts(lcolor(black) lpattern(solid)) ///
   ci1opts(lcolor(gs9) lwidth(vthin) lpattern(dash)) ///
   graphregion(fcolor(white) icolor(white) lcolor(white)) ///
   level(90)  ///
   legend(off) 
graph export FIGURE_E1.pdf, replace


 /* MODELS FOR TABLE D2 IN APPENDIX 
    - REPLACE p_majority_minority_pct WITH p_black_pct*/
 
qui mixed p_rep_pct ///
      c.u_time_CY2 ///
      c.u_time_CY2#c.u_time_CY2 ///
	  c.u_time_CY2#c.u_time_CY2#c.u_time_CY2 ///
	  c.p_black_pct ///
	  c.pct_black ///
	  c.lmasssociallib c.lmasseconlib c.lreppid_pct ///
      || state_chamber: , mle cov(unst)
estat icc
estadd scalar icc2 = r(icc2)
estimates store M1_RI_v2


qui mixed p_rep_pct ///
      c.u_time_CY2 ///
      c.u_time_CY2#c.u_time_CY2 ///
	  c.u_time_CY2#c.u_time_CY2#c.u_time_CY2 ///
	  c.p_black_pct c.p_black_pct#c.p_black_pct ///
	  c.p_black_pct#c.p_black_pct#c.p_black_pct ///
	  c.pct_black ///
	  c.lmasssociallib c.lmasseconlib c.lreppid_pct ///
      || state_chamber: , mle cov(unst)
estat icc
estadd scalar icc2 = r(icc2)
estimates store M3_RI_v2	  

qui mixed p_rep_pct ///
      c.u_time_CY2 ///
      c.u_time_CY2#c.u_time_CY2 ///
	  c.u_time_CY2#c.u_time_CY2#c.u_time_CY2 ///
	  c.p_black_pct c.p_black_pct#c.p_black_pct ///
	  c.p_black_pct#c.p_black_pct#c.p_black_pct ///
	  c.pct_black ///
	  c.lmasssociallib c.lmasseconlib c.lreppid_pct ///
      || state_chamber: u_time_CY, mle cov(unst)  
estimates store M3_RC_v2	

 esttab M1_RI_v2 M3_RI_v2 M3_RC_v2 using TABLE_D2.csv, replace ///
 nonumbers mtitles("M1" "M2" "M3") cells(b(star fmt(%9.4f)) ///
 se(par)) starlevels(+ 0.10 * 0.05 ** 0.01) stats(N aic bic icc2, ///
 fmt(%9.0g %9.3f)) varwidth(30) modelwidth(10) ///
 title("Multilevel Growth Model: % GOP Lawmakers Republican") ///
 note("Note: DV = % of lawmakers elected as GOP.") ///
 varlabels(_cons constant u_time_CY2 "Yearly Counter (0=1971)" ///
 c.u_time_CY2#c.u_time_CY2 "Yearly Counter Squared" ///
 c.u_time_CY2#c.u_time_CY2#c.u_time_CY2 "Yearly Counter Cubed" ///
 p_black_pct "% Districts w/ Black Lawmaker" ///
 c.p_black_pct#c.p_black_pct "% Districts Black Lawmaker Squared" ///
 c.p_black_pct#c.p_black_pct#c.p_black_pct "% Districts Black Lawmaker Cubed" ///
 pct_black "% States' Population Black" ///
 lmasssociallib "Mass Social Liberalism (t-1)" ///
 lmasseconlib "Mass Economic Liberalism (t-1)" ///
 lreppid_pct "% of Republican State Citizens (t-1)") ///
 drop() ///
 order(u_time_CY2 c.u_time_CY2#c.u_time_CY2 ///
 c.u_time_CY2#c.u_time_CY2#c.u_time_CY2 p_black_pct ///
 c.p_black_pct#c.p_black_pct c.p_black_pct#c.p_black_pct#c.p_black_pct) ///
 eqlabels("" "var(Chamber)" "var(Year)" "var(Beta Time)" "Corr(Time Chamber)", none) ///
 transform(ln*: exp(@)^2 exp(@)^2 at*: tanh(@) (1-tanh(@)^2)) ///
 legend style(tab) 


/* ALTERNATIVE MODELS - I.E., ERROR CORRECTION APROACH */

xtset state_chamber i_time_CY


/* 
Unit root tests for both p_rep and p_black - series' are non-stationary
*/

xtunitroot ips p_rep 
xtunitroot fisher p_rep, trend dfuller lags(1)

gen diff_p_rep = d.p_rep
xtunitroot ips diff_p_rep 
xtunitroot fisher diff_p_rep, dfuller lags(1)

xtunitroot ips p_black 
xtunitroot fisher p_black, trend dfuller lags(1)

gen diff_p_black = d.p_black
xtunitroot ips diff_p_black
xtunitroot fisher diff_p_black, dfuller lags(1)

/* Granger causality tests
Differenced series' do not granger cause eachother
requires user written program: xtgcause*/

preserve
drop if diff_p_rep==.
xtgcause diff_p_rep diff_p_black
xtgcause diff_p_black diff_p_rep
restore 


/* COINTEGRATION TESTS
series' are likely cointegrated
requires user written program: xtpedroni
*/


xtpedroni p_rep p_black



/* MODELS PRESENATED IN TABLE D1 */


encode state, gen(state_code2)

qui reg d.p_rep l.p_rep d.p_black l.p_black i.state_code2 

estimates store GECM

qui reg d.p_rep l.p_rep d.p_majority_minority l.p_majority_minority i.state_code2 

estimates store GECM_alt


 
 esttab GECM GECM_alt using TABLE_D1.csv, ar2 replace label  ///
   nonumber ///
   mtitles("GECM") ///
   varlabels(_cons Constant L.p_rep "Prop. Rep (t-1)" ///
   D.p_black "Prop. Black t-(t-1)" ///
   L.p_black "Prop. Black t-1" ///
   L.p_majority_minority "Prop. Majority Black Dist t-1" ///
   D.p_majority_minority "Prop. Majority Black Dist t-(t-1)" ///
   ) ///
   order(L.p_rep D.p_black L.p_black D.p_majority_minority ///
   L.p_majority_minority) ///
   drop(1.state_code2) ///
   cells(b(star fmt(4)) se(par fmt(4))) starlevels(+ 0.10 * 0.05 ** 0.01) ///
   stats(N aic bic) ///
   varwidth(35) style() 
 
 
/* LEBO and Taylor Grant style ecm: two step ecm*/
 
reg p_rep p_black

predict ecr, resid
 
reg d.p_rep d.p_black dl.ecr 

reg d.p_rep d.p_black i.state_code2 dl.ecr
estimates store GECM2

/*
 esttab GECM GECM2 using TABLE_D2_ALT.csv, ar2 replace  ///
   nonumber ///
   mtitles("GECM") ///
   varlabels(_cons Constant L.p_rep "% Rep (t-1)" ///
   D.p_black "% Black t-(t-1)" ///
   L.p_black "% Black t-1" ///
   ) ///
   drop(1.state_code2) ///
   cells(b(star fmt(4)) se(par fmt(4))) starlevels(+ 0.10 * 0.05 ** 0.01) ///
   stats(N aic bic) ///
   varwidth(35) style() 
*/

/*Arellano-Bond Dynamic Estimator */
xtabond p_rep p_black, lags(1) twostep noconstant vce(robust)
estat abond
/*residuals are correlated, order of 2 - need longer lag... */

xtabond p_rep p_black, lags(2) twostep noconstant vce(robust)
estat abond

 
